#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""

Spectral Energy Distributions for pySkySim.


:History:

Created on Thu Jan 22 12:00:31 2015

@author: azzollini
"""

from __future__ import absolute_import, unicode_literals, division, print_function
from pdb import set_trace as stop

from pySkySim import sed_lib as sl
from pySkySim import constants, sed_lib, losvd

import numpy as np


SpectraPath = 'data/spectra/'

def class_server(Sedtype):
    classes_dict = {'BB':BB}
    return classes_dict[Sedtype]

class Sed(object):
    """ """
        
    def __init__(self,*args,**kwargs):
        """ """
        self.path = SpectraPath
        self.hasLOSVD = False
        self.LOSVDpars = {}
        self.LOSVD = None
        self.wrange = []
        self.wavel = np.zeros(1)
        self.flambda = np.zeros(1)
        self.logwavel = np.zeros(1)
        self.flambda_logl = np.zeros(1)   
        
    
    def __call__(self,wavel,**kwargs):
        """ """
        if self.hasLOSVD:
            v = kwargs['v']
            return self.kinSEDfunct(wavel,v)
        else:
            return self.SEDfunct(wavel)
    
    
    def setLOSVD(self,sigma,h3=0.,h4=0.):
        """Modifies SED according to LOSVD: 
               - convolution with modified-gaussian kernel (v-dispersion).
               - resampling in Ln-lambda (bulk motion).
                  - the red/blue shifting only takes place at SED-evaluation.
        """
        self.hasLOSVD = True
        self.LOSVDpars['sigma'] = sigma
        self.LOSVDpars['h3'] = h3
        self.LOSVDpars['h4'] = h4
        print('WARNING: CRINGING STEP!')
        self.LOSVD = losvd.Losvd(sigma,h3,h4) # object as method!
        
        print('MISSING: convolutin with losvd') 
        # LOSVD convolution: PENDING. 
        #    sigma = self.losvd.sigma
        
        # Resampling to log-lambda for easy v-shift
        self.logresample()
        
    def logresample(self):
        """ """
        flambda = self.flambda
        wavel = self.wavel
        
        self.logwavel = np.log(wavel)
        self.flambda_logl = constants.c * sed_lib.logresample(flambda,wavel)
        print('MISSING: interpolation in log-lambda rebinning')
        ##self.inter_flambda_logl = somelinearinterpolation(self.logwavel,self.flambda_logl)

    def __str__(self):
        """ """
        return '\nI am a SED\n'
    
    def kinSEDfunct(self,wavel,v=0):
        """ """
        # interpolate (self.logwavel,self.flambda) to c*log(wavel)+v
        print('MISSING: rebinning of spectrum to log-lambda')
        shiftwavel = constants.c * np.log(wavel) + v # WRONG EXPRESSION(?)
        #flambda_logl = self.inter_flambda_logl(shiftwavel)
        print('MISSING: rebinning of log-spectrum to lambda')
        # missing: back-conversion to wavelength space.
        
        return wavel * 0.

    def loadSEDfile(self,filename):
        """ """
        self.wavel = np.zeros(1)
        self.flambda = np.zeros(1)
        
    def SEDinterpol(self,wavel):
        """ """
        wavel = self.wavel
        flambda = self.flambda
        print('MISSING: interpolation of SED')
        return wavel * 0.

class BBSed(Sed):
    """ """
    
    def __init__(self,Temp,flux=None,wref=10.):
        
        super(BBSed,self).__init__()
        self.Temp = Temp
        self.flux_ref = flux
        self.wref = wref
        
        
    def SEDfunct(self,wavel):
        """ """
        if self.flux is not None:
            return self.flux_ref * sl.BB(wavel,self.Temp) / sl.BB(self.wref,self.Temp)
        else:
            return sl.BB(wavel,self.Temp)


class StarSed(Sed):
    """ """
    def __init__(self,Sptype,flux=None,wref=10.):
        
        super(StarSed,self).__init__()
        self.Sptype = Sptype
        self.flux = flux
        self.wref = wref
        return None
        
        sed_file_name = {}
        return None
        StarSedFile = sed_file_name[Sptype]
        self.loadSEDf(StarSedFile)

    def SEDfunct(self,wavel):
        print('MISSING: interpolate SED at wavelengths')        
        return wavel * 0.
        return self.SEDinterpol(wavel)


if __name__ == '__main__':
    """Testing the sed module."""
    
    
    